package vshevchuk;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

public class ds_json extends IDS 
{
	{
		name = "JSON";
	}

	@Override
	public void Save(ArrayList<Base> p) throws Exception {
		try
		{
			BufferedWriter out = new BufferedWriter(new FileWriter("data.json"));
		    
			for (int i=0; i<p.size(); i++)	
			{
				out.write(p.get(i).toJSON());
			}
			out.close();
		}
		catch(FileNotFoundException e)
		{
        	
    	}
	}

	@Override
	public ArrayList<Base> Load() throws Exception 
	{
		String str, strFist="", strSecond="", buffer="";
		ArrayList<Base> p=null;
		
		try
		{
			BufferedReader in = new BufferedReader(new FileReader("data.json"));
			while((str = in.readLine()) != null)
			{
				strFist=str;
				buffer+=str;
				if ((strFist+strSecond).equals("{}"))
				{
					p.add(Base.CreatClass(buffer,"JSON"));
					buffer="";
				}
				strSecond=strFist;
			}	
			in.close();
		}
    	catch(FileNotFoundException e)
    	{
        	
    	}
		return p;
		
	}

}
